﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BusinessObject;

namespace YamahaTuanTrucService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BusinessService" in code, svc and config file together.
    public class BusinessService : IBusinessService
    {
        public void DoWork()
        {
        }

        public bool AddPart(PhuTung pt)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                context.PhuTung.AddObject(pt);
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }

        public PhuTung getPartByID(string id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                PhuTung pt = null;
                pt = context.PhuTung.SingleOrDefault(p => p.ID == id);
                return pt;
            }
        }
        
        public void DeletePart(PhuTung pt)
        {
            using(var context = new YamahaTuanTrucEntities())
            {
                pt.MarkAsDeleted();
                context.PhuTung.ApplyChanges(pt);
                context.SaveChanges();
            }
        }
        public bool UpdatePart(PhuTung pt)
        {
            using(var context = new YamahaTuanTrucEntities())
            {
                PhuTung tmp = context.PhuTung.SingleOrDefault(p=> p.ID == pt.ID);
                tmp = pt;
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch(Exception e)
                {
                    return false;
                }
            }
        }

        public Kho GetWareHouseByName(string tenkho)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.Kho.SingleOrDefault(p => p.Ten == tenkho);
            }
        }

        public bool UpdateRecord()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false;
                }
            }
        }

        public void DeleteAllImport(PhuTung pt)
        {
            using(var context = new YamahaTuanTrucEntities())
            {
                foreach (var import in pt.NhapHang.ToArray())
                {
                    import.MarkAsDeleted();
                }
                context.PhuTung.ApplyChanges(pt);
                context.SaveChanges();
            }
        }

        public void DeleteAllExport(PhuTung pt)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                foreach (var import in pt.XuatHang.ToArray())
                {
                    import.MarkAsDeleted();
                }
                context.PhuTung.ApplyChanges(pt);
                context.SaveChanges();
            }
        }


        public PhuTung GetPartWithImport(string id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.PhuTung.Include("NhapHang").SingleOrDefault(p => p.ID == id);
            }
        }
        public PhuTung GetPartWithExport(string id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.PhuTung.Include("XuatHang").SingleOrDefault(p => p.ID == id);
            }
        }

        public void UpdatePartWithImport(PhuTung p)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                context.PhuTung.ApplyChanges(p);
                context.SaveChanges();
            }
        }
        public void UpdatePartWithExport(PhuTung p)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                context.PhuTung.ApplyChanges(p);
                context.SaveChanges();
            }
        }

        public PhuTung[] getListPart()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.PhuTung.ToArray <PhuTung>();
            }
        }

        public NhanVien GetStaffByName(string name)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.NhanVien.SingleOrDefault(p => p.Ten == name);
            }
        }

        public bool AddMaintenance(SuaChua sc)
        {
            using(var context = new YamahaTuanTrucEntities())
            {
                context.SuaChua.AddObject(sc);
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch(Exception e)
                {
                    Console.WriteLine(e);
                    return false;
                }
            }
        }

        public SuaChua[] GetMaintenanceWithBike()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.SuaChua.Include("Xe").ToArray<SuaChua>();
            }
        }

        public SuaChua[] GetMaintenanceWithPart()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.SuaChua.Include("PhuTung").ToArray<SuaChua>();
            }
        }
        public Xe GetBikeByID(string id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.Xe.SingleOrDefault(p => p.ID == id);
            }
        }

        public bool UpdateMaintenance(SuaChua sc)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                SuaChua tmp = context.SuaChua.SingleOrDefault(p => p.ID == sc.ID);

                tmp = sc;
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false;
                }
            }
        }

        public bool DeleteMaintenance(SuaChua sc)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                SuaChua tmp = context.SuaChua.SingleOrDefault(p => p.ID == sc.ID);
                tmp.Xe.MarkAsDeleted();
                tmp.PhuTung.MarkAsDeleted();
                try
                {
                    context.SuaChua.ApplyChanges(tmp);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false;

                }
            }
        }

        public Xe[] GetBikeList()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.Xe.ToArray<Xe>();
            }
        }

        public bool AddBike(Xe x)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                context.Xe.AddObject(x);
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false;
                }
            }
        }
        public bool DeleteBike(string id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                Xe x = context.Xe.SingleOrDefault(p => p.ID == id);
                if (x != null)
                {
                    x.MarkAsDeleted();
                    context.Xe.ApplyChanges(x);
                    try
                    {
                        context.SaveChanges();
                        return true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }

        public bool UpdateBike(Xe x)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                Xe tmp = context.Xe.SingleOrDefault(p => p.ID == x.ID);
                tmp = x;
                context.Xe.ApplyChanges(tmp);
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false;
                }
            }
        }

        public NhanVien[] getListStaff()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.NhanVien.ToArray<NhanVien>();
            }
        }

        public KhachHang[] GetListCustomer()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.KhachHang.ToArray<KhachHang>();
            }
        }

        public Xe[] GetListBike()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.Xe.ToArray<Xe>();
            }
        }

        public SuaChua[] GetListMaintenance()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                return context.SuaChua.ToArray<SuaChua>();
            }
        }
    }
}
